package com.bristua.barcode.supplier.biz;

import com.bristua.barcode.common.exception.BizException;
import com.bristua.barcode.common.util.DateUtils;
import com.bristua.barcode.common.util.SequenceUtil;
import com.bristua.barcode.supplier.dao.TChannelMapper;
import com.bristua.barcode.supplier.dao.TQrCodeMapper;
import com.bristua.barcode.supplier.enums.SupplierCode;
import com.bristua.barcode.supplier.model.TChannel;
import com.bristua.barcode.supplier.model.TQrCode;
import com.bristua.barcode.supplier.model.condition.AddSupplierQrcodeParams;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * Created by Administrator on 2019/8/30.
 */
@Service
@Transactional
public class AddSupplierQrcodeBiz {

    @Resource
    private TQrCodeMapper tQrCodeMapper;

    @Resource
    private TChannelMapper tChannelMapper;

    public void addSupplierQrcode(AddSupplierQrcodeParams addSupplierQrcodeParams){
        String channelId = addSupplierQrcodeParams.getChannelId();
        TChannel tChannel = tChannelMapper.selectByPrimaryKey(channelId);
        if(tChannel!=null && tChannel.getStatus().intValue()==1){
            if(tChannel.getChannelType().intValue()==1){
                if(StringUtils.isEmpty(addSupplierQrcodeParams.getCodeAmt())){
                   throw new BizException(SupplierCode.QRCODE_AMT.getCode(),SupplierCode.QRCODE_AMT.getMsg());
                }
            }
        }else{
            throw new BizException(SupplierCode.QRCODE_AMT.getCode(),SupplierCode.QRCODE_AMT.getMsg());
        }
        TQrCode tQrCode = new TQrCode();
        tQrCode.setCreateTime(DateUtils.getShowDateTime());
        tQrCode.setQrCodeId(SequenceUtil.nextId());
        tQrCode.setQrCodeUrl(addSupplierQrcodeParams.getQrCodeUrl());
        tQrCode.setStatus(0);
        tQrCode.setChannelId(channelId);
        tQrCode.setSupplierId(addSupplierQrcodeParams.getSupplierId());

        if(!StringUtils.isEmpty(addSupplierQrcodeParams.getCodeAmt())){
            long amt = new BigDecimal(addSupplierQrcodeParams.getCodeAmt()).multiply(new BigDecimal(100)).longValue();
            tQrCode.setCodeAmt(amt);
        }
        if(!StringUtils.isEmpty(addSupplierQrcodeParams.getUserName())){
            tQrCode.setUserName(addSupplierQrcodeParams.getUserName());
        }

        if(!StringUtils.isEmpty(addSupplierQrcodeParams.getPassword())){
            tQrCode.setPassword(addSupplierQrcodeParams.getPassword());
        }

        tQrCodeMapper.insert(tQrCode);
    }
}
